package com.demoview.test.myapplication.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.demoview.test.myapplication.MessageBean;
import com.demoview.test.myapplication.R;
import com.demoview.test.myapplication.SingleSocket;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import io.socket.client.Socket;
import io.socket.emitter.Emitter;

/**
 *
 * @author zhengshaorui
 * @date 2019/6/14
 */

public class LoginFragment extends Fragment {
    private static final String TAG = "LoginFragment";

    public static LoginFragment newInstance() {

        Bundle args = new Bundle();

        LoginFragment fragment = new LoginFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private Socket mSocket;
    private RecyclerView mRecyclerView;
    private MessageBean msgBean;

    @SuppressLint("WrongConstant")
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = LayoutInflater.from(getContext()).inflate(R.layout.login_layout,container,false);
        //获取服务器地址
        getServerUrl();

        mRecyclerView = view.findViewById(R.id.messages);
        return view;
    }

    private Emitter.Listener broadcastingListener = new Emitter.Listener() {
        @SuppressLint("WrongConstant")
        @Override
        public void call(Object... args) {
            JSONArray data = (JSONArray) args[0];
            Log.d(TAG,data.toString());
            System.out.print(data.toString());


            for (int i=0;i<data.length();i++){
                try {
                    String a= data.getString(i);
                    Log.d(TAG,a);

                    Gson gson = new Gson();
                    msgBean = gson.fromJson(a, new TypeToken<MessageBean>() {
                     }.getType());
                    List<MessageBean.MsgBean> dataBeans=msgBean.getMsg();


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }


        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        mSocket.disconnect();
        mSocket.off("broadcastingListen",broadcastingListener);
        mSocket.off(Socket.EVENT_CONNECT);
    }

    /**
     * 获取服务器地址
     */
    private void getServerUrl(){
                OkGo.<String>get("https://livenew.yunbaozb.com/appapi/?service=Live.enterRoom")
                .params("uid", "46390")
                .params("token", "e00a89db593dbac0fa78ec0b1fccb6f0")
                .params("city", "%E6%B3%B0%E5%AE%89%E5%B8%82")
                .params("liveuid", "34043")
                .params("mobileid", "811fd947e528b51f6d1687c766a194d4")
                .params("stream", "34043_1627468319")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        System.out.print("成功==" + response);
                        mSocket = SingleSocket.getInstance().getSocket();

                        //注册 login 事件
                        mSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
                            @Override
                            public void call(Object... args) {
                                Log.d(TAG, "zsr call: ");
                                org.json.JSONObject data=new org.json.JSONObject();
                                try {
                                    data.put("uid","46390");
                                    data.put("token","e00a89db593dbac0fa78ec0b1fccb6f0");
                                    data.put("mobileid","811fd947e528b51f6d1687c766a194d4");
                                    data.put("liveuid","34043");
                                    data.put("roomnum","34043");
                                    data.put("stream","34043_1627468319");

                                    //发送
                                    mSocket.emit("conn",data);

                                }catch(JSONException e){
                                    e.printStackTrace();
                                }

                            }
                        });

                        mSocket.on("broadcastingListen",broadcastingListener);
                        mSocket.connect();


                        }

                });

    }



}
